// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()

/**
 * 依据id查询公告信息
 */
exports.main = async (event, context) => {
  const { OPENID } = cloud.getWXContext()
  const id = event.id
  const res = await db.collection('notice')
    .doc(id)
    .get()
  if (res.errMsg === 'document.get:ok') {
    return {
      code: 0,
      data: res.data,
      isAuthor: OPENID === res.data._openid
    }
  } else {
    return {
      code: -1
    }
  }
}